﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TravianHelper.hero;
using TravianHelper.oasis;
using System.Collections;

namespace TravianHelper.app
{
    /**
     * Store common data of the application such as Hero.
     * Most of the data are singleton
     **/ 
    public class TravianApplicationContext
    {
        public static bool loggedIn = false;
        public static bool accountBlocked = false;
        public static HeroInfo heroInfo;
        public static HeroConfig heroConfig;
        public static HeroManager heroManager;
        public static OasisManager oasisManager;

        public static Travian adapter;
        public static HeroPopup heroPopup;
        public static Form2 mainForm;

        public static ArrayList villages = new ArrayList();
        public static ArrayList allOasis = new ArrayList();

        public static Hashtable oasisPool = new Hashtable();

        public static Hashtable errors = new Hashtable();

        public static ArrayList allNatars = new ArrayList();

        public TravianApplicationContext()
        {
            // 1. Contruct object
            adapter = new Travian();
            heroPopup = new HeroPopup();
            heroManager = new HeroManager();
            oasisManager = new OasisManager();
        }

        public static void loadData()
        {
            heroConfig = heroManager.loadHeroConfigFromForm();
            if (heroConfig == null)
                heroConfig = new HeroConfig();
            try
            {
                TravianApplicationContext.villages = Util.DeSerializeArrayList("data.xml");
            }
            catch (Exception ex)
            {
                Console.WriteLine("error " + ex);
                TravianApplicationContext.villages = new ArrayList();
            }
            try
            {
                TravianApplicationContext.allOasis = Util.DeSerializeArrayList("oasis.xml");              
            }
            catch (Exception ex)
            {
                Console.WriteLine("error " + ex);
                TravianApplicationContext.allOasis = new ArrayList();
            }

            try
            {
                TravianApplicationContext.oasisPool = new Hashtable();
                ArrayList temp= Util.DeSerializeArrayList("oasisPool.xml");
                foreach (Zone oasis in temp)
	            {
                    TravianApplicationContext.oasisPool.Add(oasis.x + "," + oasis.y, oasis);
                }
                 
            }
            catch (Exception ex)
            {
                Console.WriteLine("error " + ex);
                TravianApplicationContext.oasisPool = new Hashtable();
            }

            // 2. load Hero info
            heroInfo = heroManager.loadHeroInfo();
        }
    }
}
